Public Partial Class AddEditPost
    Inherits System.Web.UI.Page
    Private forumID As String = String.Empty
    Private threadID As String = String.Empty
    Private postID As String = String.Empty
    Private quotePostID As String = String.Empty
    Private isNewThread As Boolean = False
    Private isNewReply As Boolean = False
    Private isEditingPost As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' retrieve the querystring parameters
        forumID = Request.QueryString("ForumID")
        If Not String.IsNullOrEmpty(Request.QueryString("ThreadID")) Then
            threadID = Request.QueryString("ThreadID")
            If Not String.IsNullOrEmpty(Request.QueryString("QuotePostID")) Then
                quotePostID = Request.QueryString("QuotePostID")
            End If
        End If
        If Not String.IsNullOrEmpty(Me.Request.QueryString("PostID")) Then
            postID = Request.QueryString("PostID")
        End If

        isNewThread = (String.IsNullOrEmpty(postID) = True) And (String.IsNullOrEmpty(threadID) = True)
        isEditingPost = Not (String.IsNullOrEmpty(postID))
        isNewReply = (Not isNewThread And Not isEditingPost)

        ' show/hide controls and load data according to the parameters above
        If Not Me.IsPostBack Then
            Dim isModerator As Boolean = (Me.User.IsInRole("Administrators") Or _
                Me.User.IsInRole("Editors") Or Me.User.IsInRole("Moderators"))

            lnkThreadList.NavigateUrl = String.Format(lnkThreadList.NavigateUrl, forumID)
            lnkThreadPage.NavigateUrl = String.Format(lnkThreadPage.NavigateUrl, threadID)
            chkClosed.Visible = isNewThread

            If isEditingPost Then
                ' load the post to edit, and check that the current user has the
                ' permission to do so
                Dim post As KDESolutions.Providers.Forums.DataTypes.PostDetails = KDESolutions.Providers.Forums.ForumService.GetPostByID(postID)
                If Not isModerator AndAlso _
                    Not (Me.User.Identity.IsAuthenticated And _
                    Me.User.Identity.Name.Equals(post.AddedBy.ToLower)) Then
                End If

                lblEditPost.Visible = True
                btnSubmit.Text = "Update"
                txtTitle.Text = post.Title
                txtBody.Text = post.Body
                panTitle.Visible = isModerator
            ElseIf isNewReply Then
                ' chech whether the thread the user is adding a reply to is still open
                Dim post As KDESolutions.Providers.Forums.DataTypes.PostDetails = KDESolutions.Providers.Forums.ForumService.GetPostByID(threadID)
                If post.Closed Then
                    Throw New ApplicationException( _
                        "The thread you tried to reply to has been closed.")
                End If

                lblNewReply.Visible = True
                txtTitle.Text = "Re: " & post.Title
                lblNewReply.Text = String.Format(lblNewReply.Text, post.Title)
                ' if the ID of a post to be quoted is passed on the querystring, load
                ' that post and prefill the new reply's body with that post's body
                If String.IsNullOrEmpty(quotePostID) = False Then
                    Dim quotePost As KDESolutions.Providers.Forums.DataTypes.PostDetails = KDESolutions.Providers.Forums.ForumService.GetPostByID(quotePostID)
                    txtBody.Text = String.Format( _
                        "<blockquote><hr noshade="""" size=""1"" />" & _
                        "<b>Originally posted by {0}</b><br /><br />{1}" & _
                        "<hr noshade="""" size=""1"" /></blockquote>", _
                        quotePost.AddedBy, quotePost.Body)
                End If
            ElseIf isNewThread Then
                lblNewThread.Visible = True
                lnkThreadList.Visible = True
                lnkThreadPage.Visible = False
            End If
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If isEditingPost Then
            ' when editing a post, a line containing the current Date/Time and the
            ' name of the user making the edit is added to the post's body so that
            ' the operation gets logged
            Dim body As String = txtBody.Text
            body &= String.Format("<p>-- {0}: post edited by {1}.</p>", _
                DateTime.Now.ToString, Me.User.Identity.Name)
            ' edit an existing post
            Dim editPost As New KDESolutions.Providers.Forums.DataTypes.PostDetails
            editPost.ID = postID
            editPost.Title = txtTitle.Text
            editPost.Body = body
            editPost.AddedBy = Context.User.Identity.Name
            If (Not (Context.Request.ServerVariables("HTTP_VIA")) Is Nothing) Then
                editPost.AddedByIP = Context.Request.ServerVariables("HTTP_X_FORWARDED_FOR").ToString
            Else
                editPost.AddedByIP = Context.Request.ServerVariables("REMOTE_ADDR").ToString
            End If
            editPost.AddedDate = Now
            editPost.Approved = True
            editPost.Closed = False
            editPost.LastPostBy = Context.User.Identity.Name
            editPost.LastPostDate = Now()

            KDESolutions.Providers.Forums.ForumService.UpdatePost(editPost)
            panInput.Visible = False
            panFeedback.Visible = True
        Else
            ' insert the new post
            Dim newPost As New KDESolutions.Providers.Forums.DataTypes.PostDetails
            newPost.ID = postID
            newPost.Title = txtTitle.Text
            newPost.Body = txtBody.Text
            newPost.AddedBy = Context.User.Identity.Name
            If (Not (Context.Request.ServerVariables("HTTP_VIA")) Is Nothing) Then
                newPost.AddedByIP = Context.Request.ServerVariables("HTTP_X_FORWARDED_FOR").ToString
            Else
                newPost.AddedByIP = Context.Request.ServerVariables("REMOTE_ADDR").ToString
            End If
            newPost.AddedDate = Now
            newPost.Approved = True
            newPost.Closed = chkClosed.Checked
            newPost.LastPostBy = Context.User.Identity.Name
            newPost.LastPostDate = Now()
            newPost.ForumID = Request.QueryString("ForumID")
            newPost.ViewCount = 0
            If isNewThread = False Then
                newPost.ParentPostID = threadID
            End If
            KDESolutions.Providers.Forums.ForumService.InsertPost(newPost)
            panInput.Visible = False
            HttpContext.Current.Profile.SetPropertyValue("Posts", (HttpContext.Current.Profile.GetPropertyValue("Posts") + 1))
            Dim forum As KDESolutions.Providers.Forums.DataTypes.ForumDetails = KDESolutions.Providers.Forums.ForumService.GetForumByID(forumID)
            If forum.Moderated Then
                If Not Me.User.IsInRole("Administrators") AndAlso _
                    Not Me.User.IsInRole("Editors") AndAlso _
                    Not Me.User.IsInRole("Moderators") Then
                    panApprovalRequired.Visible = True
                Else
                    panFeedback.Visible = True
                End If
            Else
                panFeedback.Visible = True
            End If
            End If
    End Sub
End Class